package com.xiaoxu.content.controller;

import com.xiaoxu.base.exception.bkSchoolException;
import com.xiaoxu.base.model.PageParams;
import com.xiaoxu.base.model.PageResult;
import com.xiaoxu.content.model.dto.AddCourseDto;
import com.xiaoxu.content.model.dto.CourseBaseInfoDto;
import com.xiaoxu.content.model.dto.EditCourseDto;
import com.xiaoxu.content.model.dto.QueryCourseParamsDto;
import com.xiaoxu.content.model.po.CourseBase;
import com.xiaoxu.content.service.CourseBaseService;
import com.xiaoxu.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoxu
 * @version 1.0
 * @description 课程相关信息接口
 * @date 2023/1/15 14:48
 */
@RestController
@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
public class CourseBaseInfoController {

    private final CourseBaseService courseBaseService;

    public CourseBaseInfoController(CourseBaseService courseBaseService) {
        this.courseBaseService = courseBaseService;
    }

    @ApiOperation("查询课程分页信息接口")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody QueryCourseParamsDto queryCourseParams){
        return courseBaseService.queryCourseList(pageParams, queryCourseParams);
    }

    @ApiOperation("新增课程相关信息")
    @PostMapping("/course")
    public CourseBaseInfoDto addCourse(@RequestBody @Validated AddCourseDto dto){
        //获取当前用户所属的培训机构Id
        //取出当前用户身份
        SecurityUtil.User user = SecurityUtil.getUser();
        if (user == null){
            throw new bkSchoolException("用户解析出错了，请检查是否非法使用，如无法解决，请联系管理员");
        }
        Long companyId = Long.getLong(user.getCompanyId());
        //调用service
        return courseBaseService.addCourse(companyId, dto);
    }

    @ApiOperation("根据课程id查询课程相关信息接口")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable Long courseId){
        //获取当前用户所属的培训机构Id
        //取出当前用户身份
        SecurityUtil.User user = SecurityUtil.getUser();
        if (user == null){
            throw new bkSchoolException("用户解析出错了，请检查是否非法使用，如无法解决，请联系管理员");
        }
        System.err.println(user);
        return courseBaseService.queryCourseInfoById(courseId);
    }

    @ApiOperation("根据课程id修改课程相关信息接口")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto dto){
        //获取当前用户所属的培训机构Id
        //取出当前用户身份
        SecurityUtil.User user = SecurityUtil.getUser();
        if (user == null){
            throw new bkSchoolException("用户解析出错了，请检查是否非法使用，如无法解决，请联系管理员");
        }
        Long companyId = Long.getLong(user.getCompanyId());
        //调用service
        return courseBaseService.updateCourse(companyId, dto);
    }





}
